#include "lyricswindow.h"
#include "ui_lyricswindow.h"

#include "trackController.h"
#include "track.h"
#include "lyricsReader.h"

// Qt
#include <QFileSystemModel>
#include <QSortFilterProxyModel>
#include <QSettings>
#include <phonon>

FileFilterProxyModel::FileFilterProxyModel(QObject *parent) :
    QSortFilterProxyModel(parent)
{

}

bool FileFilterProxyModel::filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const
{
    QModelIndex index0 = sourceModel()->index(sourceRow, 0, sourceParent);
    QFileSystemModel* fileModel = qobject_cast<QFileSystemModel*>(sourceModel());

    QSettings settings;
    const QString rootDir = settings.value("rootDir").toString();

    QFileInfo info(fileModel->filePath(index0));
    QString filePath = info.absoluteFilePath();
    return (filePath.startsWith(rootDir) && (info.isDir() || filePath.endsWith(".mp3"))) || rootDir.startsWith(filePath);
    // uncomment to call the default implementation
    //return QSortFilterProxyModel::filterAcceptsRow(sourceRow, sourceParent);
}


LyricsWindow::LyricsWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::LyricsWindow),    
    trackController(new TrackController(this)),
    currentTrack(new Track),
    lyricsReader(new LyricsReader(this))
{
    // setup QtDesigner UI
    ui->setupUi(this);

    // setup track controller
    ui->trackControllerContainer->layout()->addWidget(trackController);

    // setup lyrics reader
    ui->lyricsTab->layout()->addWidget(lyricsReader);

    // connect slots
    connect(currentTrack, SIGNAL(trackChanged(Track*)), this, SLOT(trackChanged(Track*)));    
    connect(currentTrack, SIGNAL(trackChanged(Track*)), lyricsReader, SLOT(trackChanged(Track*)));
    connect(lyricsReader, SIGNAL(lyricsLoaded(Track*)), trackController, SLOT(trackChanged(Track*)));
    connect(trackController, SIGNAL(positionChanged(double)), lyricsReader, SLOT(positionChanged(double)));

    QFileSystemModel *sourceModel = new QFileSystemModel(this);
    QSettings settings;
    QString rootDir = settings.value("rootDir").toString();
    sourceModel->setRootPath(rootDir);

    QSortFilterProxyModel *proxyModel = new FileFilterProxyModel(this);

    proxyModel->setSourceModel(sourceModel);
    ui->treeView->setModel(proxyModel);
    ui->treeView->hideColumn(1);
    ui->treeView->hideColumn(2);
    ui->treeView->hideColumn(3);

    ui->treeView->setRootIndex(proxyModel->mapFromSource(sourceModel->index(rootDir)));
}

LyricsWindow::~LyricsWindow()
{    
    delete ui;    
}

void LyricsWindow::on_actionExit_triggered()
{
    QMainWindow::close();

}

void LyricsWindow::on_actionOptions_triggered()
{
    optionsDialog = new OptionsDialog(this);
    optionsDialog->show();
}

void LyricsWindow::on_treeView_clicked(QModelIndex index)
{
    FileFilterProxyModel* proxyModel = dynamic_cast<FileFilterProxyModel*>(ui->treeView->model());
    QFileSystemModel* fileModel = qobject_cast<QFileSystemModel*>(proxyModel->sourceModel());
    QFileInfo fileInfo(fileModel->filePath(proxyModel->mapToSource(index)));

    if (fileInfo.isFile()) {
        currentTrack->setPath(fileInfo.absoluteFilePath());
    }
}

void LyricsWindow::trackChanged(Track *track)
{
    ui->titleLabel->setText(track->getTitle());
    ui->artistLabel->setText(track->getArtist());
    ui->albumLabel->setText(track->getAlbum());
    ui->yearLabel->setText(QString::number(track->getYear()));

    ui->coverLabel->setPixmap(track->getCover());
}
